
package rentalStore;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import rentalCompany.Quote;
import rentalCompany.Reservation;
import rentalCompany.ReservationConstraints;
import rentalCompany.ReservationException;

/**
 *
 * @author
 */
public class ClientSession implements ClientSessionRemote {
    
    private Set<Quote> quotes = new HashSet<Quote>();
    private final String clientName;
    
    public ClientSession(String name) {
        this.clientName = name;
    }

    public Quote createQuote(Date startDate, Date endDate, String carType, String company) 
            throws RemoteException, ReservationException {
        Quote quote = CarRentalStore.getRentals().get(company).createQuote(new ReservationConstraints(startDate, endDate, carType), this.clientName);
        quotes.add(quote);
        return quote;
    }

    public Set<Quote> getCurrentQuotes() throws RemoteException {
        return quotes;
    }

    public void confirmQuotes() throws RemoteException, ReservationException {
        Set<Reservation> confirmedRs = new HashSet<Reservation>();
        try {
            for(Quote q: quotes) {
                confirmedRs.add(CarRentalStore.getRentals().get(q.getRentalCompany()).confirmQuote(q));
            }
            quotes.clear();
        } catch(ReservationException re) {
            for(Reservation r: confirmedRs) {
                CarRentalStore.getRentals().get(r.getRentalCompany()).cancelReservation(r);
            }
            throw re;
        }
    }
    
}
